package com.wontube.web.verify;

import java.util.ArrayList;
import java.util.List;


/**
 * 验证参数结果
 * @author tanggf(2012-5-7)
 *
 */
public class VerifyResult {
	/** 验证结果集 */
	private List<InvalidField> invalidFields = new ArrayList<InvalidField>();
	private String message = null;
	private boolean isDone;		
	
	public boolean isDone(){
		isDone = (invalidFields.size() == 0);
		return isDone;
	}
	
	public void addInvalidField(String message){
		addInvalidField(null, message);
	}	
	public void addInvalidField(String message, Object[] args){
		addInvalidField(null, message, args);
	}
	public void addInvalidField(String field, String message){
		addInvalidField(new InvalidField(field, message));
	}
	public void addInvalidField(String field, String message, Object[] args){
		addInvalidField(new InvalidField(field, message, args));
	}	
	public void addInvalidField(InvalidField field){
		invalidFields.add(field);
	}		
	public List<InvalidField> getInvalidFields(){
		return invalidFields;
	}	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	public String getResultMessage(){
		if(isDone()){
			return this.message;
		}else{
			return getErrorMessages();
		}
	}

	/**
	 * 返回全部验证信息(逗号分隔)
	 * @param separator
	 * @return
	 */
	public String getErrorMessages(){		
		return getErrorMessages(", ");
	}
	
	/**
	 * 返回全部验证信息
	 * @param separator
	 * @return
	 */
	private String getErrorMessages(String separator){
		if( ! isDone()){
			StringBuilder sb = new StringBuilder();
			for (InvalidField field : invalidFields) {
				sb.append(field.getMessage()).append(separator);
			}
			return sb.substring(0, sb.length() - separator.length());			
		}
		return "";
	}
}
